package com.xiubai.appforpda.webutils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Handler;
import android.os.Message;

import com.google.myjson.Gson;
import com.google.myjson.JsonObject;
import com.xiubai.appforpda.entity.SQLMainPamInventBean;
import com.xiubai.appforpda.jack.utils.AppConstants;
import com.xiubai.appforpda.jack.utils.JsonParser;
import com.xiubai.appforpda.jack.utils.Trace;
import com.xiubai.appforpda.jack.utils.WebServiceDataUtils;
import com.xiubai.appforpda.jack.utils.WebServiceUtils;

public class WebServiceHelper {
    // 正式地址
//    public static final String WEB_URL =  "http://"+ AppConstants.IP_ADDRESS +":"+AppConstants.PORT_ADDRESS+"/uapws/service/nc.pub.shht_zc.itf.IshhtZCItf";
    // 测试
    //public static final String WEB_URL = "http://192.168.43.235:8081/uapws/service/nc.web.test.itf.ItfWebSMService";
    public static final String WEB_URL = "http://"+ AppConstants.IP_ADDRESS +":"+AppConstants.PORT_ADDRESS+"/uapws/service/nc.web.test.itf.ItfWebSMService";

    private static final ExecutorService executorService = Executors
            .newFixedThreadPool(3);
    //    private static final String NAMESPACE = "http://itf.shht_zc.pub.nc/IshhtZCItf/";// "http://WebXml.com.cn/";
    // 测试
    private static final String NAMESPACE = "http://itf.test.web.nc/ItfWebSMService/";// "http://WebXml.com.cn/";

    public static final int timeout =  60 * 1000;

    public static void callWebService(String url, final String methodName,
                                      LinkedHashMap<String, String> properties,
                                      final WebServiceCallBack webServiceCallBack) {
        final HttpTransportSE httpTransportSE = new HttpTransportSE(url, timeout);
        SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
        if (properties != null) {
            for (Iterator<Map.Entry<String, String>> it = properties.entrySet()
                    .iterator(); it.hasNext(); ) {
                Map.Entry<String, String> entry = it.next();
                Trace.d("key=" + entry.getKey() + "value=" + entry.getValue());
                soapObject.addProperty(entry.getKey(), entry.getValue());
            }
        }
        final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER10);
        soapEnvelope.setOutputSoapObject(soapObject);
        soapEnvelope.dotNet = false;
        httpTransportSE.debug = true;

        final Handler mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                webServiceCallBack.callBack((SoapObject) msg.obj);
            }

        };

        executorService.submit(new Runnable() {

            @Override
            public void run() {
                SoapObject resultSoapObject = null;
                try {
                    httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);
                    if (soapEnvelope.getResponse() != null) {
                        resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
//                        Trace.d("resultSoapObject=" + resultSoapObject);
                    }
                } catch (HttpResponseException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                } finally {
                    mHandler.sendMessage(mHandler.obtainMessage(0,
                            resultSoapObject));
                }
            }
        });
    }

    public static void callWebServiceSynchronized(String url, final String methodName,
                                                  LinkedHashMap<String, String> properties,
                                                  final WebServiceCallBack webServiceCallBack) {
        final HttpTransportSE httpTransportSE = new HttpTransportSE(url, timeout);
        SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
        if (properties != null) {
            for (Iterator<Map.Entry<String, String>> it = properties.entrySet()
                    .iterator(); it.hasNext(); ) {
                Map.Entry<String, String> entry = it.next();
                Trace.d("key=" + entry.getKey() + "value=" + entry.getValue());
                soapObject.addProperty(entry.getKey(), entry.getValue());
            }
        }
        final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER10);
        soapEnvelope.setOutputSoapObject(soapObject);
        soapEnvelope.dotNet = false;
        httpTransportSE.debug = true;
        SoapObject resultSoapObject = null;
        try {
            httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);
            if (soapEnvelope.getResponse() != null) {
                resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
            }
        } catch (HttpResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } finally {
            webServiceCallBack.callBack(resultSoapObject);
        }
    }

    public interface WebServiceCallBack {
        public void callBack(SoapObject result);
    }

}
